package com.youcheng.config.rabbit;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/*发布订阅模式*/
/*现在生产者发送了一条消息，会发现consumer1，2都会收到。之前不是说过一个队列里面的一条消息，只能被一个消费者消费吗？怎么现在一条消息被两个消费者消费了。要知道这里对于生产者来说是只生产了一条消息，但是它发送给了交换机，交换机会根据绑定的队列来发送。现在绑定了queueA，queueB队列，所以两个队列里面都有消息了。而消费者关注的也是两个队列，就看到了一条消息被两个消费者消费的情况了。*/
@Configuration
public class RabbitConfigPublish {

    @Bean
    public Queue queueA(){
        return new Queue("helloA" , true);
    }

    @Bean
    public Queue queueB(){
        return new Queue("helloB" , true);
    }

    //创建一个fanoutExchange交换机
    @Bean
    public FanoutExchange fanoutExchange() {
        return new FanoutExchange("fanoutExchange");
    }

    //将queueA队列绑定到fanoutExchange交换机上面
    @Bean
    public Binding bindingExchangeMessageFanoutA(Queue queueA, FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(queueA).to(fanoutExchange);
    }

    //将queueB队列绑定到fanoutExchange交换机上面
    @Bean
    public Binding bindingExchangeMessageFanoutB(Queue queueB, FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(queueB).to(fanoutExchange);
    }


}
